package com.example.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;

import javax.management.openmbean.TabularData;

/**
 * 返回内容
 */
public class AppResult<T> {
    //状态码
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private int code;
    //描述信息
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String meassge;
    //具体的数据
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;

    /**
     * 构造方法
     * @param code
     * @param meassge
     */
    public AppResult(int code, String meassge) {
        this(code,meassge,null);
    }

    public AppResult(int code, String meassge, T data) {
        this.code = code;
        this.meassge = meassge;
        this.data = data;
    }

    /**
     * success
     * @return
     */
    public static AppResult success(){
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }
    public static AppResult success(String meassge){
        return new AppResult(ResultCode.SUCCESS.getCode(), meassge);
    }
    public static <T>AppResult<T> success(T data){
        return new AppResult<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data);
    }
    public static <T>AppResult<T>success(String meassge,T data){
        return  new AppResult<>(ResultCode.SUCCESS.getCode(),meassge,data);
    }

    /**
     * failed
     * @return
     */
    public static AppResult failed(){
        return new AppResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }
    public static AppResult failed(String meassge){
        return new AppResult(ResultCode.FAILED.getCode(), meassge);
    }
    public static <T>AppResult<T> failed(T data){
        return new AppResult<>(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage(),data);
    }
    public static <T>AppResult<T>failed(String meassge,T data){
        return  new AppResult<>(ResultCode.FAILED.getCode(),meassge,data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMeassge() {
        return meassge;
    }

    public void setMeassge(String meassge) {
        this.meassge = meassge;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
